#include "securitysetting.h"
#include "ui_securitysetting.h"





SecuritySetting::SecuritySetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SecuritySetting)
{
    ui->setupUi(this);


//    cliSocket = new QUdpSocket(this);

//    connect(cliSocket, SIGNAL(readyRead()),
//            this, SLOT(readPendingDatagrams()));

     state = 0;
//    timer = new QTimer(this);
//    connect(timer, SIGNAL(timeout()),
//            this, SLOT(timeSlot()));



    //windows
    camera = new QCamera(this);
    //this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
    viewfinder=new QCameraViewfinder(ui->label_video3);
    //ui->label_video3->setScaledContents(true);
    viewfinder->resize(ui->label_video3->size());
    CamerimageCapture=new QCameraImageCapture(camera);
    CamerimageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    camera->setCaptureMode(QCamera::CaptureStillImage);//将其采集为图片
    camera->setCaptureMode(QCamera::CaptureMode::CaptureViewfinder);//将采集到取景器中
    //QCameraViewfinderSettingsControl set;
    //set.setResolution(1280, 720);
    camera->setViewfinder(viewfinder);
    //viewfinder->show();
    //camera->start();
    connect(CamerimageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage)));
    //camera->stop();
    //viewfinder->close();

}

SecuritySetting::~SecuritySetting()
{
    delete ui;
}

void SecuritySetting::readPendingDatagrams()
{
//    while (cliSocket->hasPendingDatagrams()) {  //如果缓冲区有至少一个数据包，那么返回真
//        QByteArray datagram;
//        datagram.resize(cliSocket->pendingDatagramSize());  //重设接收缓冲区大小为待读取的字节数

//        cliSocket->readDatagram(datagram.data(), datagram.size(),
//                                NULL, NULL);

//        //业务逻辑
//        qDebug()<<"size = "<<datagram.size();

//        QPixmap map;
//        if(map.loadFromData(datagram,"JEPG")){
//            ui->label_video1->setPixmap(map);
//        }
//    }

}

void SecuritySetting::on_startEdit_clicked()
{
    if(state == 0)
    {
        //启动定时器
        //timer->start(100);
        //ui->startEdit->setText("停止监控");//开始录像按钮
        viewfinder->show();
        camera->start();
        state = 1;
    }
    else
    {
        //timer->stop();
        //ui->startEdit->setText("开始监控");
        camera->stop();
        viewfinder->close();
        state = 0;
    }
}

void SecuritySetting::timeSlot()
{
//    cliSocket->writeDatagram("get stream", \
//                             QHostAddress(ui->ipEdit->text()), \
//                             ui->portEdit->text().toUShort());
//ipEdit 输入框输入ip地址
//portEdit 输入框输入端口
}


void SecuritySetting::on_takePhoto_clicked()
{

   CamerimageCapture->capture();
   //displayImage();


}




//windows

void SecuritySetting::captureImage()
{
    CamerimageCapture->capture();
}

void SecuritySetting::displayImage(int, QImage image)
{
    ui->label_video4->setPixmap(QPixmap::fromImage(image));
    ui->label_video4->setScaledContents(true);
}

//void SecuritySetting::on_pushButton_clicked()
//{
////    const QPixmap* pixmap=ui->label_video4->pixmap();
////        if(pixmap) {
////            pixmap->save("paizhao.jpg");
////            FaceImg = "paizhao.jpg";
////            QString strCurrentPath=QDir::currentPath();
////            qDebug()<<strCurrentPath;
////            close();
////        }
//    captureImage();

//}



void SecuritySetting::on_save_clicked()
{
    QString strCurrentPath=QDir::currentPath();
    qDebug()<<"保存路径:"<<strCurrentPath;
}

void SecuritySetting::on_clear_clicked()
{
    //取消预览照片
    ui->label_video4->clear();
}
